var basePath = "http://59.111.95.231:80/";
var viewer, scene, canvas, ellipsoid, camera, tileset, aTileset = {}, aObj = {}, aDatasource = {}, aLayer = {}, sImagery, sTerrain, resourceTitle;
//单体化
var attachPolygonDataSource = undefined;

//场景
var sceneid = request.QueryString("guid", false, "42603ab2-8e0d-430c-9708-0a8028786412");
$(".sceneId").val(sceneid);
//地形
var defaultTerrain;

//滚动条
$(".setting-panel-scroll").mCustomScrollbar();
$("#markerpanel .audiobigBox").mCustomScrollbar();
$(window).resize(function () {
    $(".setting-panel-scroll").mCustomScrollbar("update");
});

/***************************工具栏、操作****************************/
//预览
$("#previewLink").attr("href", "http://59.111.95.231:80/LSGlobe/scene.html?guid=" + sceneid);


var baseSetting = {
    newRandomGuid: function () {
        var guid = "";
        for (var i = 1; i <= 32; i++) {
            var n = Math.floor(Math.random() * 16.0).toString(16);
            guid += n;
            if ((i == 8) || (i == 12) || (i == 16) || (i == 20))
                guid += "-";
        }
        return guid;
    },
    jugePoint: function (modelInfo, point2) {
        //point1 模型中心点  r 模型半径
        //point2添加点 
        var isInModel = false;
        var height1 = 6371004 + modelInfo.center.z;
        var x1 = height1 * Math.cos(modelInfo.center.x) * Math.cos(modelInfo.center.y);
        var y1 = height1 * Math.cos(modelInfo.center.x) * Math.sin(modelInfo.center.y);
        var z1 = height1 * Math.sin(modelInfo.center.x);
        var height2 = 6371004 + point2.z;
        var x2 = height2 * Math.cos(point2.x) * Math.cos(point2.y);
        var y2 = height2 * Math.cos(point2.x) * Math.sin(point2.y);
        var z2 = height2 * Math.sin(point2.x);
        var L = Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2) + Math.pow(z1 - z2, 2));
        if (L < modelInfo.radius) {
            isInModel = true;
        }
        return isInModel;
    },
    subJson: function (subtype) {
        //本页面配置
        var datas = {};
        datas.guid = sceneid;
        datas["sceneJson"] = JSON.stringify(viewer.toJSON());

        var url = "http://59.111.95.231:80/TX_SceneManager/scene/sceneJsonUpload.action";
        $.ajax(
        {
            url: url,
            type: "post",
            data: datas,
            async: false,
            dataType: "Json",
            success: function (result) {
                if (result.resultCode == "1") {
                    if (!!!subtype) {
                        $('body').dailog({ type: 'danger', bottons: ['确定'], discription: "保存成功" });
                    }

                } else {
                    $('body').dailog({ type: 'danger', bottons: ['确定'], discription: result.resultMsg });
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $('body').dailog({ type: 'danger', bottons: ['确定'], discription: "tojson失败" });
            }
        });
    },
    wordLimit: function () {
        $(".zxx_text_overflow").each(function () {
            var copyThis = $(this.cloneNode(true)).hide().css({
                "position": "absolute",
                "width": "auto",
                "overflow": "visible"
            });
            $(this).after(copyThis);
            if (copyThis.width() >= $(this).parent().width()) {
            } else {
                $(this).removeAttr("title");
            }
        });
    },
    init: function () {
        $("#savaScene").click(function () {
            var saveModel = $("#lblSaveModel").val();
            //设置基本信息
            switch (saveModel) {
                case "infopanel":
                    sceneInfo.save("notip");
                    sceneInfoToolSet.save();
                    sceneInfoVideo.save("all");
                    break;
                case "mappanel":
                    break;
                case "layerpanel":
                    break;
                case "markpanel":
                    break;
                case "monomerpanel":
                    dantihua.commitAttach(true);
                    break;
                case "widgetpanel":
                    widget.uploadWidget(true);
                    break;
                case "pushpanel":
                    //压平
                    flat.save(true);
                    break;
                case "waterpanel":
                    water.uploadWaterData();
                    break;
                case "flypanel":
                    break;
                case "Screenpanel":
                    screenCompare.uploadScreen();
                    break;
                case "thumpanel":
                    break;
                case "viewpospanel":
                    break;
            }
            baseSetting.subJson();
        });

        //飞行设置拖拽
        $(document).on("mousemove", "canvas", function (e) {
            var floatTip = $("#float-tip");
            if (isNotInTileSet) {
                floatTip.html("请依附模型进行操作").css({ left: e.clientX + 15 + "px", top: e.clientY + "px" });
                floatTip.show();
                return;
            }
            /* if(isNotInTileSet){
            floatTip.html("请依附模型进行操作").css({left:e.clientX+15+"px",top:e.clientY+"px"});
            floatTip.show();
            return;
            } */
            if (isAddMarker) {
                floatTip.html("左击添加标注").css({ left: e.clientX + 15 + "px", top: e.clientY + "px" });
                floatTip.show();
            } else if (isAttachDraw) {
                floatTip.html("左击绘制,右击结束").css({ left: e.clientX + 15 + "px", top: e.clientY + "px" });
                floatTip.show();
            } else if (isPoint) {
                floatTip.html("点击添加点").css({ left: e.clientX + 15 + "px", top: e.clientY + "px" });
                floatTip.show();
            } else if (isPolygon_fill || isPolyline) {
                floatTip.html("左击开始, 右击结束").css({ left: e.clientX + 15 + "px", top: e.clientY + "px" });
                floatTip.show();
            } else if (isFlat) {
                floatTip.html("左击绘制压平面，右击结束").css({ left: e.clientX + 15 + "px", top: e.clientY + "px" });
                floatTip.show();
            }
        });
    }
}